import { localSet, localGet, localRemove } from '@/common/utils/GlobalUtils'
import loginApi from "@/api/login";
import {filterAsyncRouter} from "@/utils/createRoutes";

// 菜单显示全局变量
const state = {
  showMenuFlag: true,
  activePath: ''|| localGet('ActiveMenuPath'),
  leftMenu: [],
  breadcrumbItemsArr: [], // 面包屑用
  saveData: [],// 存储编辑数据
}

const mutations = {
  SET_MENU_DISPLAY (state, value) {
    state.showMenuFlag = !state.showMenuFlag
  },
  // 存储当前点击的菜单
  SET_ACTIVE_PATH (state, value) {
    state.activePath = value
    localSet('ActiveMenuPath',value)
  },
  RESET_ACTIVE_PATH: state => {
    localRemove('ActiveMenuPath')
    state.activePath = ''
  },
  // 菜单内容设置
  SetLeftMenu: (state,value) => {
    state.leftMenu = value;
  },
  GetLeftMenu: (state) => {
    // state.leftMenu = localGet('routes') || []
  },
  // 设置面包屑
  SET_BREAD_ITEMS (state, value) {
    // console.log('传进store的path值：',value);
    if (state.breadcrumbItemsArr.length > 0) {
      let pathArr = state.breadcrumbItemsArr.map((n,i) => {
        return n.path
      })
      let pathNewArr = new Set(pathArr)
      if (!pathNewArr.has(value.path)) {
        state.breadcrumbItemsArr.push(value)
      }
    } else {
      state.breadcrumbItemsArr.push(value)
    }
  },
  // 删除面包屑
  DELET_BREAD_ITEMS (state, index) {
    if (state.breadcrumbItemsArr.length > 1) {
      state.breadcrumbItemsArr.splice(index,1);
    }
  },
  DELETE_BREAD_ITEMS_ALL:(state,value)=>{
    state.breadcrumbItemsArr = [{path: value.path,title:value.title}];
  },
  menu_clear:(state)=>{
    state.leftMenu = [];
    state.breadcrumbItemsArr = [];
  },
  SAVE_STATE: (state, params) => {
    // console.log(params, 'SAVE_STATE');
    if (state.saveData.length) {
      state.saveData.map(item => {
        if (item.name === params.name) {
          item.value = params.value;
        } else {
          state.saveData.push(params);
        }
      })
    } else {
      state.saveData.push(params);
    }
    // console.log(state.saveData, '===saveData===');
  },
  DELETE_STATE: (state) => {
    console.log('DELETE_STATE');
    state.saveData = [];
  }
}

export default {
  state,
  mutations
}
